﻿using DTBuilderLib.VisualHierarchyClasses;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Drawing;
using DTBuilderLib;

namespace t_DTBuilderLib
{
    
    
    /// <summary>
    ///This is a test class for VisualBitmapImageTest and is intended
    ///to contain all VisualBitmapImageTest Unit Tests
    ///</summary>
    [TestClass()]
    public class VisualBitmapImageTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for VisualBitmapImage Constructor
        ///</summary>
        [TestMethod()]
        public void VisualBitmapImageConstructorTest()
        {
            float xinches = 11F;
            float yinches = 8.5F;
            int dpi = 600;
            Color c = Color.Black;
            VisualBitmapImage target = new VisualBitmapImage(xinches, yinches, dpi, c);
            Assert.IsTrue(target.Width == xinches, "Width is not right");
            Assert.IsTrue(target.Height == yinches, "Height is not right");
            Assert.IsTrue(target.XPosition == 0, "x position is not right");
            Assert.IsTrue(target.YPosition == 0, "y position is not right");

            ///
            /// Next, must check the DPI of the image
            /// 

            VisualHolder_Accessor vh = new VisualHolder_Accessor();
            vh.AddImage(target);
            var info = vh.SGNodeList;
            Assert.IsTrue(info.Count == 1, "Wrong number of images came back!");
            FileInfo f = new FileInfo(info[0].FileName);
            Assert.IsTrue(f.Exists, "Output image does not exist");
            int[] pixels = new int[2];
            pixels = TestUtilities.GetImageSize(f);
            Assert.IsTrue(pixels[0] - (int)(xinches*(float)dpi) < 2, "Image is not right width");
            Assert.IsTrue(pixels[1] - (int)(yinches*(float)dpi) < 2, "Image is not right height");

            Assert.IsTrue(info[0].Height == yinches, "Height is not right!");
            Assert.IsTrue(info[0].Width == xinches, "Width is wrong");
            Assert.IsTrue(info[0].x == 0, "x position of sg is not right");
            Assert.IsTrue(info[0].y == 0, "y position of sg is not right");
            Assert.IsTrue(info[0].ZOrder == 1, "Zorder is not right in sg");

            Assert.IsTrue(c.ToArgb() == TestUtilities.GetPixelColor(1,1,f).ToArgb(), "Color of image is not right");
        }

        /// <summary>
        ///A test for IncZIndex
        ///</summary>
        [TestMethod()]
        public void IncZIndexTest()
        {
            float xinches = 8F;
            float yinches = 11F;
            int dpi = 600;
            VisualBitmapImage target = new VisualBitmapImage(xinches, yinches, dpi, Color.Black); // TODO: Initialize to an appropriate value
            int delta = 2;

            target.IncZIndex(delta);

            VisualHolder_Accessor vh = new VisualHolder_Accessor();
            vh.AddImage(target);
            var info = vh.SGNodeList;

            Assert.IsTrue(info[0].ZOrder == 3, "Zorder is not right in sg");
        }

        /// <summary>
        ///A test for Scale
        ///</summary>
        [TestMethod()]
        public void ScaleTest()
        {
            float xinches = 11.0F;
            float yinches = 8.5F;
            int dpi = 600;
            VisualBitmapImage target = new VisualBitmapImage(xinches, yinches, dpi, Color.Black); // TODO: Initialize to an appropriate value
            float factor = 0.5F; // TODO: Initialize to an appropriate value
            target.Scale(factor);
            Assert.IsTrue(target.Width == xinches * factor, "Width is not correct");
            Assert.IsTrue(target.Height == yinches * factor, "Height is not correct");
            Assert.IsTrue(target.XPosition == 0.0F, "X position is not zero");
            Assert.IsTrue(target.YPosition == 0.0F, "Y Position is not zero");

            ///
            /// Next, must check the DPI of the image
            /// 

            VisualHolder_Accessor vh = new VisualHolder_Accessor();
            vh.AddImage(target);
            var info = vh.SGNodeList;
            Assert.IsTrue(info.Count == 1, "Wrong number of images came back!");
            FileInfo f = new FileInfo(info[0].FileName);
            Assert.IsTrue(f.Exists, "Output image does not exist");
            int[] pixels = new int[2];
            pixels = TestUtilities.GetImageSize(f);
            Assert.IsTrue(pixels[0] == xinches*dpi, "Image is not right width");
            Assert.IsTrue(pixels[1] == yinches*dpi, "Image is not right height");
        }

        [TestMethod()]
        public void TestAddingText()
        {
            VisualBitmapImage bmi = new VisualBitmapImage(11.0F, 8.5F, 300, Color.Blue);
            bmi.AddText(0.0F, 0.0F, 0.5F, "hi there");
            var info1 = bmi.SGNodeList;

            /// If we add text now we shoudl throw because the jpeg has already been generated.
            try
            {
                bmi.AddText(0.0F, 0.5F, 0.5F, "no way");
                Assert.Fail("AddTExt should have thrown an exception because we were adding some text after jpeg");
            }
            catch (DTException)
            {
            }

            ///
            /// Now, check the output jpeg. Don't know how to check for text, so we just check
            /// to see if something was "written" out.
            /// 

            Assert.IsFalse (TestUtilities.AllPixelsAreOfColor(new FileInfo(info1[0].FileName), 0, 0, 11*300-1, 300-1, Color.Blue), "The image is solid in color - is there writing?");
        }

        [TestMethod]
        public void TestOutputDir()
        {
            string localdir = System.Environment.CurrentDirectory;
            Assert.IsFalse(VisualBitmapImage.GlobalImageWritingDirectory.FullName == localdir, "Directory wasn't saved correctly");
            VisualBitmapImage.GlobalImageWritingDirectory = new DirectoryInfo(localdir);
            Assert.IsTrue(VisualBitmapImage.GlobalImageWritingDirectory.FullName == localdir, "Directory wasn't saved correctly");
            VisualBitmapImage newimage = new VisualBitmapImage(1.0F, 1.0F, 300, Color.Red);
            Assert.IsTrue(newimage.ImageWritingDirectory.FullName == localdir, "The created image isn't writing to the correctly dir");

            var info1 = newimage.SGNodeList;
            Assert.IsTrue(info1[0].FileName.StartsWith(localdir), "Created jpeg image doesn't seem to be in the right place");
        }

        [TestMethod]
        public void TestZIndex()
        {
            string localdir = System.Environment.CurrentDirectory;
            VisualBitmapImage.GlobalImageWritingDirectory = new DirectoryInfo(localdir);
            VisualBitmapImage newimage = new VisualBitmapImage(1.0F, 1.0F, 300, Color.Red);

            Assert.AreEqual(1, newimage.ZIndex, "An unset z index should be 1");
            newimage.IncZIndex();
            Assert.AreEqual(2, newimage.ZIndex, "inced z shoudl be 2");
        }
    }
}
